// pages/tabBar/my/component/Information/infomation.js
import { request } from '../../../../../utils/util'
const { $Message } = require('../../../../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},  //获取用户信息
    code: "",       //提前储存code
    visible: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        that.setData({
          ...that.data,
          userInfo: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    wx.login({
      success: res => {
        that.setData({
          ...that.data,
          code: res.code
        })
      }
    })
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        that.setData({
          ...that.data,
          userInfo: res.data
        })
      }
    })
  },


  /**
   * 修改名字click
   */
  nameClick: function () {
    wx.navigateTo({
      url: `../Information/updateInfo/updateInfo?type=name&param=${this.data.userInfo.first_name}`,
    })
  },
  /**
   * 修改手机号click
   */
  phoneClick: function () {
    wx.navigateTo({
      url: `../Information/updateInfo/updateInfo?type=phone&param=${this.data.userInfo.phone}`,
    })
  },
  /**
   * 已绑定的微信
   */
  wxClickAlready: function () {
    this.setData({
      ...this.data,
      visible: true
    })
  },
  /**
   * 解绑微信提示框的取消按钮
   */
  handleClose() {
    this.setData({
      ...this.data,
      visible: false
    })
  },
  /**
   * 确定解绑微信的确定按钮
   * @param {*} e 
   */
  handleOk() {
    const that = this
    request("PATCH", "/api/auth/wx/", "", "", true, "解绑中")
      .then(res => {
        if (res.statusCode === 200) {
          that.setData({
            ...this.data,
            visible: false
          })
          request("GET", `/api/auth/user/${wx.getStorageSync('userInfo').id}/`)
            .then(res => {
              that.setData({
                ...that.data,
                userInfo: res.data.data
              })
              wx.setStorageSync('userInfo', res.data.data)
              $Message({
                content: '解绑微信成功',
                type: 'success'
              });

            })
        }
        that.onShow()
      })
  },

  /**
   * 未绑定的微信
   */
  bindGetUserInfo(e) {
    const that = this
    wx.getUserProfile({
      desc: '用于标识个人头像',
      success: res => {
        const userRes = res
        const data = {
          code: that.data.code
        }
        request("GET", "/api/auth/wx/", data, "", true, "解析中")
          .then(res => {
            if (res.data.token) {
              if (res.data.errcode === 40163) {
                $Message({
                  content: '该微信已绑定其他账户',
                  type: 'warning'
                });
                return
              }
              const userInfo = e.detail.userInfo

              const data = {
                data : userRes
              }
              request("PUT", `/api/auth/wx/?token=${res.data.token}`, data, "", true, "绑定中").then(res => {

                if (res.statusCode === 200) {

                  if (res.data.code === 200) {
                    $Message({
                      content: '绑定成功',
                      type: 'success'
                    });

                    request("GET", `/api/auth/user/${wx.getStorageSync('userInfo').id}/`)
                      .then(res => {
                        that.setData({
                          ...that.data,
                          userInfo: res.data.data
                        })
                        wx.setStorageSync('userInfo', res.data.data)

                      })
                  } else if (res.data.code === 214) {
                    $Message({
                      content: '该微信已绑定其他账户',
                      type: 'warning'
                    });
                  }
                } else if (res.statusCode === 420) {
                  const msg = res.data.errMsg
                  $Message({
                    content: msg,
                    type: 'warning'
                  });
                }
              }).catch(res => {

              })

            } else {
              return
            }
            that.onShow()
          }

          )
      },
      fail: res => {

      }
    })
    
  },
})